/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GlbRtbEng-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_26			*/
/*	File:		GlbRtbRedsTest.cpp		*/
/********************************************************/

#include <stdarg.h>

#include "GlbRtbReds.h"

static char* sg_pszField[] = {
	(char*)"start",
	(char*)"end",
	(char*)"browser",
	(char*)"os",
	(char*)"location",
	(char*)"mediatype",
	(char*)"media",
	(char*)"priority",
	(char*)"volume",
	(char*)"price",
	(char*)"url",
	(char*)"screen",
	(char*)"type"
};

void GlbRtbRedsSet(GLBRDCON_P pstCon)
{
	UINT uiKey = 0;
	char szKey[GLB_BYTE16] = {0};
	char* pszValue[] = {
		(char*)"2016-1-1 10:00",
		(char*)"2016-1-10 12:00",
		(char*)"IE8,IE9",
		(char*)"windows xp,windows7,windows8",
		(char*)"0086-0010-0010,0086-0010-0011,0086-0010-0012,0086-0010-0013,0086-0010-0014,0086-0010-0015",
		(char*)"2",
		(char*)"tanx",
		(char*)"1",
		(char*)"10000",
		(char*)"1000",
		(char*)"http://www.baidu1.com",
		(char*)"1",
		(char*)"280*280"
	};

	for (; uiKey < 10; uiKey ++) {
		sprintf(szKey, "%03d", uiKey);
		GLB_RD_RCMD(pstCon, "HMSET %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
			szKey,
			sg_pszField[0], pszValue[0],
			sg_pszField[1], pszValue[1],
			sg_pszField[2], pszValue[2],
			sg_pszField[3], pszValue[3],
			sg_pszField[4], pszValue[4],
			sg_pszField[5], pszValue[5],
			sg_pszField[6], pszValue[6],
			sg_pszField[7], pszValue[7],
			sg_pszField[8], pszValue[8],
			sg_pszField[9], pszValue[9],
			sg_pszField[10], pszValue[10],
			sg_pszField[11], pszValue[11],
			sg_pszField[12], pszValue[12]
		);
	}

	return;
}

void GlbRtbRedsGet(GLBRDCON_P pstCon)
{
	char* pszKey = NULL;
	UINT uiKey = 0, uiVal = 0;
	GLBRDREP_P pstKey = NULL, pstVal = NULL;

	if (NULL != (pstKey = (GLBRDREP_P)GlbRdScan(pstCon))) {
		while (uiKey < GLB_RD_RTTL(pstKey)) {
			pszKey = GLB_RD_RVAL(pstKey, uiKey);
			GLB_PRINT("Key[%d]: %s\n", uiKey ++, pszKey);
			if (NULL != (pstVal = (GLBRDREP_P)GLB_RD_RCMD(pstCon, "HMGET %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
					pszKey,
					sg_pszField[0],
					sg_pszField[1],
					sg_pszField[2],
					sg_pszField[3],
					sg_pszField[4],
					sg_pszField[5],
					sg_pszField[6],
					sg_pszField[7],
					sg_pszField[8],
					sg_pszField[9],
					sg_pszField[10],
					sg_pszField[11],
					sg_pszField[12]
				))) {
				for (uiVal = 0; uiVal < GLB_RD_RTTL(pstVal); uiVal ++) {
					GLB_PRINT("%s: %s\n", sg_pszField[uiVal], GLB_RD_RVAL(pstVal, uiVal));
				}
				GLB_RD_RFRO(pstVal);
			}
			else {
				GLB_PRINT("Failure to GLB_RD_RCMD\n");
			}
			GLB_PRINT("\n");
		}
		GLB_RD_RFRO(pstKey);
	}

	return;
}

int main(int argc, char* argv[])
{
	GLBRDCON_P pstCon = NULL;

	if (NULL != (pstCon = GLB_RD_RCNT("127.0.0.1", 6379))) {
		GlbRtbRedsSet(pstCon);
		GlbRtbRedsGet(pstCon);
		GLB_RD_RFRE(pstCon);
	}

	return GLB_SUCCESS;
}
